package com.example.yls.webtest;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    TextView responseText;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
          setTitle("webTest");
          Button search=(Button)findViewById(R.id.search);
          search.setOnClickListener(this);
        //
        Button sendRequest=(Button)findViewById(R.id.send_request);
        responseText=(TextView)findViewById(R.id.response_text);
          sendRequest.setOnClickListener(this);
        Button sendRequestOkHttp=(Button)findViewById(R.id.send_request_okhttp);
                           sendRequestOkHttp.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.search:
            Intent searchIntent = new Intent(this, SearchActivity.class);
            startActivity(searchIntent);
                break;
            case  R.id.send_request:
                sendRequestWithHttpURLConnection();
                break;
            case R.id.send_request_okhttp:
                sendRequestWithOkhttp();
                break;
                default:
                    break;

        }
    }

    private void sendRequestWithOkhttp() {
        Log.e("try","555");
          new Thread(
                  new Runnable() {

                      public void run() {
                          try{
                              OkHttpClient client=new OkHttpClient();
                              Request request=new Request.Builder().url("http://www.baidu.com").build();
                              Response response=client.newCall(request).execute();
                              String responseData=response.body().toString();
                              showResponse(responseData);
                          }catch(Exception e){
                              e.printStackTrace();
                          }
                      }
                  }
          ).start();





    }

    private void sendRequestWithHttpURLConnection() {
      new Thread(
          new Runnable(){

              public void run() {
                  HttpURLConnection connection=null;
                  BufferedReader reader=null;
                  try{
                      URL url=new URL("http://www.baidu.com");
                      connection=(HttpURLConnection)url.openConnection();
                        connection.setRequestMethod("GET");
                        connection.setConnectTimeout(8000);
                        connection.setReadTimeout(8000);
                      InputStream in=connection.getInputStream();
                      reader =new BufferedReader(new InputStreamReader(in));
                      StringBuilder response=new StringBuilder();
                         String line;
                         while((line=reader.readLine())!=null){
                             response.append(line);}
                       showResponse(response.toString());
                  }catch (Exception e){
                      e.printStackTrace();
                  }finally {
                      if(reader!=null){
                          try {
                              reader.close();
                          }catch (IOException e){
                              e.printStackTrace();
                          }
                      }
                      if (connection!=null){connection.disconnect();}
                  }


              }
          }).start();
    }

    private void showResponse(final String response) {
        runOnUiThread(new Runnable() {

            public void run() {
                responseText.setText(response);
            }
        });
    }


}
